﻿using PSTUTemplate.Models.DBObject;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Web.WebPages.Html;

namespace PSTUTemplate.Models
{
    public class Repository
    {
        PSTUDBEntities _db = new PSTUDBEntities();
        //PSTULinqDataContext _db = new PSTULinqDataContext();

        public int AddStudent(StudentEnrol aStudent)
        {
            _db.StudentEnrols.AddObject(aStudent);
            Save();
            return aStudent.Id;
        }

        internal void AdDFIRemoval(FIRemoval fi)
        {
            _db.FIRemovals.AddObject(fi);
            Save();

        }

        internal void AddProfile(Profile newProfile)
        {
            _db.Profiles.AddObject(newProfile);
            Save();
        }

        public int AddMarkSheet(ExamMarkSheet viewModels)
        {
            _db.ExamMarkSheets.AddObject(viewModels);
            Save();
            return viewModels.Id;
        }

        public void Save()
        {
            try
            {
                _db.SaveChanges();
            }
            catch (Exception ex)
            {
                string exc = ex.Message + "Stack=" + ex.StackTrace;

            }
        }
        public IQueryable<StudentEnrol> GetStudentList()
        {
            var clist = from list in _db.StudentEnrols
                        select list;
            return clist;
        }

        public IQueryable<Course> GetCourseList()
        {
            var clist = from list in _db.Courses
                        select list;
            return clist;
        }

        internal List<SelectListItem> GetCourseListAsSelectListItem()
        {
            List<SelectListItem> listItems = new List<SelectListItem>();

            SelectListItem list = new SelectListItem();

            foreach (var item in GetCourseList())
            {
                list = new SelectListItem();

                list.Value = item.Id.ToString();
                list.Text = item.Title;

                listItems.Add(list);

            }
            return listItems;
        }

        internal IQueryable<Course> GetCourseListBylevelAndSemester(int? level, int? semester)
        {

            var clist = from list in _db.Courses
                        where list.Level == level && list.Semester == semester
                        select list;
            return clist;

        }

        internal IQueryable<Profile> GetProfileList()
        {
            var lists = from list in _db.Profiles
                        select list;
            return lists;
        }

        public IQueryable<ExamResult> GetResultList()
        {
            var lists = from list in _db.ExamResults
                        select list;
            return lists;
        }

       
        internal StudentEnrol GetAStudentById(int id)
        {
            StudentEnrol aStudent = _db.StudentEnrols.SingleOrDefault(x => x.Id == id);
            return aStudent;
        }

        internal Profile GetProfileByLoggedUserName(string loggeduser)
        {

            Profile myProfile = _db.Profiles.FirstOrDefault(x => x.LoggedUser == loggeduser);

            return myProfile;
        }
        internal Profile GetAProfileByUserId(int id)
        {
            return _db.Profiles.FirstOrDefault(x => x.UserId == id);
        }
        internal StudentEnrol GetAStudentEnrolByUserId(int id)
        {
            return _db.StudentEnrols.FirstOrDefault(x => x.UserId == id);
        }

       
        internal void DeleteAStudentById(int id)
        {
            StudentEnrol aStudent = GetAStudentById(id);
            _db.StudentEnrols.DeleteObject(aStudent);
            Save();
        }


        internal StudentEnrol GetAStudentEnrolById(int userId)
        {
            return _db.StudentEnrols.FirstOrDefault(x => x.UserId== userId);
        }
    }
}